<template>
  <div class="show_cloth_container">
    <div id="show_cloth">
      <ul>
        <li
          v-for="(i, index) in title"
          :key="index"
          @click="
            changeTab(index);
            getNeedProducts(index);
          "
          :class="[index == tabIndex?'add':'']"
        >
          {{ i }}
        </li>
      </ul>
      <div v-if="tabIndex == 0">
        <div
          class="cloth_detail animate__animated animate__fadeInDown"
          v-for="item in product.slice(0, 8)"
          :key="item.goods_id"
          style="animation-duration: 500ms"
        >
          <div class="img_box" @click="toProductDetails(item)">
            <img :src="item.goods_img" />
          </div>
          <p>{{ item.goods_title }}</p>
          <p>￥{{ item.goods_price }}元</p>
        </div>
      </div>
      <div v-else-if="tabIndex == 1">
        <div
          class="cloth_detail animate__animated animate__fadeInDown"
          style="animation-duration: 500ms"
          v-for="item in product.slice(0, 8)"
          :key="item.goods_id"
        >
          <div class="img_box" @click="toProductDetails(item)">
            <img :src="item.goods_img" />
          </div>
          <p>{{ item.goods_title }}</p>
          <p>￥{{ item.goods_price }}元</p>
        </div>
      </div>
      <div v-else>
        <div
          class="cloth_detail animate__animated animate__fadeInDown"
          style="animation-duration: 500ms"
          v-for="item in product.slice(0, 8)"
          :key="item.goods_id"
        >
          <div class="img_box" @click="toProductDetails(item)">
            <img :src="item.goods_img" />
          </div>
          <p>{{ item.goods_title }}</p>
          <p>￥{{ item.goods_price }}元</p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "HomeShowCloth",
  data() {
    return {
      tabIndex: 0,
      title: ["热销推荐", "最多收藏", "最多浏览"],
      product: [],
    };
  },
  methods: {
    changeTab(index) {
      this.tabIndex = index;
    },
    getNeedProducts(index) {
      this.product = [];
      if (index == 1) {
        this.$axios.get("/api/product/getProductByCollection").then((res) => {
          this.product = res.data.data;
        });
      } else if (index == 2) {
        this.$axios.get("/api/product/getProductByBrowse").then((res) => {
          this.product = res.data.data;
        });
      } else {
        this.$axios.get("/api/product/getProductBySales").then((res) => {
          this.product = res.data.data;
        });
      }
    },
    toProductDetails(item) {
      this.$router.push({
        path: "/productdetails",
        query: { id: item.goods_id },
      });
    },
  },
  created() {
    this.getNeedProducts();
  },
};
</script>
<style scoped>
.show_cloth_container {
  width: 100%;
  height: 800px;
}

#show_cloth {
  width: 1170px;
  height: 100%;
  margin: 60px auto;
}

#show_cloth ul {
  display: flex;
  justify-content: center;
}

#show_cloth li {
  width: 200px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  float: left;
  border-radius: 10px;
}

#show_cloth li:hover {
  color: #fff;
  background-color: #4e6ef2;
  cursor: pointer;
}
.add{
   color: #fff;
  background-color: #4e6ef2;
}

#show_cloth > div {
  width: 100%;
  height: 600px;
  clear: both;
  margin: 10px auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.cloth_detail {
  width: 200px;
  height: 300px;
  margin: 20px;
}

.cloth_detail .img_box {
  position: relative;
  width: 200px;
  height: 200px;
  overflow: hidden;
}

.img_box img {
  position: absolute;
  width: 100%;
  object-fit: cover;
  transition: all 0.6s;
}

.cloth_detail p {
  font-size: 13px;
  margin-top: 10px;
  height: 40px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  word-break: break-all;
}

.cloth_detail p + p {
  font-size: 16px;
  color: red;
  text-align: center;
}

.img_box img:hover {
  transform: scale(1.1);
}
</style>